﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace PDChart
{
    
    public partial class PRPDChart2D : PDChart
    {
        public PRPDChart2D()
        {
           
        }

        public override bool LoadFile(string filename)
        {
            PDFile pdf = new PDFile();

            try
            {
                pdf.Load(filename, 12);

                Set(pdf.ToPRPD(50, 100));

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        protected override void OnDraw(Graphics g)
        {

           
            float w = (float)this.ClientRectangle.Width;
            float h = (float)this.ClientRectangle.Height;
          
                

            DrawText(g);

            PaletteLegend pal = new PaletteLegend(new PointF(w * 0.02f, h * 0.15f), new SizeF(w * 0.03f, h * 0.4f), this.LabelFont, "0", "50");
            
            pal.Draw(g);

            Grid2D grid = new Grid2D(new PointF(w * 0.25f, h * 0.15f), new SizeF(w * 0.7f, h * 0.7f), this.LabelFont, GetCurrentAmp());

            grid.DrawBackground(g);

            MaxValue = new XYZ(200, 50, 50);


            for (int i = 0; i < Data.GetLength(0); i++)
            {
                for (int j = 0; j < Data.GetLength(1); j++)
                {
                    if (Data[i, j] > 0)
                    {
                        float mz = (MaxValue.Z == 0) ? 1 : MaxValue.Z;

                        Color color = pal.GetColor(Data[i, j] * GetCurrentAmp() / mz);

                        using (SolidBrush brush = new SolidBrush(color))
                        {
                            float mx = (MaxValue.X == 0) ? 1 : MaxValue.X;

                            float my = (MaxValue.Y == 0) ? 1 : MaxValue.Y;

                            PointF loc = grid.GetPosition(i / mx, (j + 1) / my);

                            g.FillRectangle(brush, new RectangleF(loc, new SizeF(grid.Size.Width / mx, grid.Size.Height / my)));
                        }
                    }

                }
            }


            base.OnDraw(g);
        }


    }


    internal class Grid2D
    {
        public Color BackColor = Color.DarkGray;
        public Color LineColor = Color.White;
        public Color FontColor = Color.White;
        public Font Font;
        public PointF Location;
        public SizeF Size;

        public double Amp = 1;

        public Grid2D(PointF location, SizeF size,  Font font, double amp)
        {
            Location = location;
            Size = size;
            Font = font;
            Amp = amp;
        }


        public PointF GetPosition(float x, float y)
        {

            float y0 = Location.Y + Size.Height - (float)(y * this.Size.Height);
            float x0 = Location.X + (float)(x * this.Size.Width);

            return new PointF(x0, y0);
        }

        public PointF GetPosition(PointF p)
        {
            return GetPosition(p.X, p.Y);
        }
        

        protected void DrawYLabel(Graphics g, string str, float x, float y)
        {
            SizeF sz = g.MeasureString(str, this.Font);

            using (SolidBrush brush = new SolidBrush(FontColor))
            {
                g.DrawString(str, this.Font, brush, x - sz.Width, y - sz.Height / 2);
            }
        }
        protected void DrawXLabel(Graphics g, string str, float x, float y)
        {
            SizeF sz = g.MeasureString(str, this.Font);

            using (SolidBrush brush = new SolidBrush(FontColor))
            {
                g.DrawString(str, this.Font, brush, x - sz.Width / 2, y + sz.Height / 4);
            }
        }
        

        public void DrawBackground(Graphics g)
        {
          
            int nx = 4;
            int ny = 4;

            using (SolidBrush brush = new SolidBrush(BackColor))
            {
                g.FillRectangle(brush, new RectangleF(Location, Size));
            }


            
            using (Pen pen = new Pen(Color.LightGray, 1))
            {
                PointF[] pts = new PointF[(int)Math.Ceiling(this.Size.Width)];

                for (int i = 0; i < pts.Length; i++)
                {
                    pts[i] = new PointF(Location.X + i, Location.Y + Size.Height / 2 - Size.Height / 2 * (float)Math.Sin((double)i / pts.Length * 2 * Math.PI));
                }

                g.DrawLines(pen, pts);
            }



            using (Pen pen = new Pen(new SolidBrush(Color.White)))
            {

                for (int i = 0; i <= ny; i++)
                {
                    
                    g.DrawLine(pen, new PointF(Location.X, Location.Y + Size.Height / ny * i), new PointF(Location.X + Size.Width, Location.Y + Size.Height / ny * i));
                }

                for (int i = 0; i <= nx; i++)
                {
                    g.DrawLine(pen, new PointF(Location.X + Size.Width / nx * i, Location.Y ), new PointF(Location.X + Size.Width / nx * i, Location.Y + Size.Height));
                }

                

            }

            
            double[] lx = { 0,  0.25, 0.5, 0.75, 1 };
            string[] ly = { "0", "+90", "0", "-90", "0" };


            using (SolidBrush brush = new SolidBrush(FontColor))
            {

                for (int i = 0; i < lx.Length; i++)
                {
                    DrawYLabel(g, (lx[i] / Amp * 100).ToString() + "%" , Location.X, Location.Y + Size.Height - Size.Height * i / ny);
                }

                for (int i = 0; i < ly.Length; i++)
                {
                    DrawXLabel(g, ly[i], Location.X + Size.Width * i / nx, Location.Y + Size.Height);
                }
            }




        }

    }
}
